<template>
  <view class="container">
    <!-- <view class="alert">
      <text>物品放行</text>
    </view> -->
    <float-button name="passPermit" />
    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        err-show-type="toast"
      >
        <uni-forms-item label="申请事由" required name="applicationReason">
          <uni-easyinput
            v-model="formData.applicationReason"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="物品所属单位" required name="goodsOwnUnit">
          <uni-easyinput v-model="formData.goodsOwnUnit" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="物品所属人" required name="goodsOwnPerson">
          <uni-easyinput
            v-model="formData.goodsOwnPerson"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="司机姓名" required name="driverName">
          <uni-easyinput v-model="formData.driverName" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="司机电话" required name="driverPhone">
          <uni-easyinput v-model="formData.driverPhone" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="车牌号" required name="carNumber">
          <uni-easyinput v-model="formData.carNumber" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="附件" name="oss">
          <FileUploader v-model="formData.oss" />
        </uni-forms-item>
        <uni-forms-item label="备注" name="described">
          <uni-easyinput
            type="textarea"
            v-model="formData.described"
            placeholder="请输入"
          />
        </uni-forms-item>
        <!-- 动态表单 -->
        <view class="divider divider-horizontal">
          <view class="text">以下是明细列表</view>
        </view>
        <button
          type="default"
          style="
            color: #ffffff;
            background-color: #e6a23c;
            border-color: #1aad19;
          "
          size="mini"
          @click="add"
        >
          新增明细项
        </button>
        <uni-table border emptyText="暂无数据" style="margin-bottom: 30rpx">
          <!-- 表头行 -->
          <uni-tr>
            <uni-th width="50">操作</uni-th>
            <uni-th>物品名称</uni-th>
            <uni-th>规格型号</uni-th>
            <uni-th>单位</uni-th>
            <uni-th>描述</uni-th>
          </uni-tr>
          <!-- 表格数据行 -->
          <uni-tr v-for="(item, i) in tableData" :key="i">
            <uni-td>
              <uni-icons
                type="trash-filled"
                size="30"
                color="#F56C6C"
                @click="del(i)"
              ></uni-icons>
            </uni-td>
            <uni-td>{{ item.goodsName }}</uni-td>
            <uni-td>{{ item.specificationModel }}</uni-td>
            <uni-td>{{ item.unit }}</uni-td>
            <uni-td>{{ item.described }}</uni-td>
          </uni-tr>
        </uni-table>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>

    <uni-popup ref="popup">
      <GoodsForm @callback="onAddGoods" />
    </uni-popup>
  </view>
</template>

<script>
import {
  addPassPermit,
  netCompleteTask,
  netStartWorkFlow,
  netPassPermitDetail,
} from '@/api/workflow';
import GoodsForm from './goodsForm.vue';
import FileUploader from '@/components/FileUploader/index.vue';
export default {
  components: {
    GoodsForm,
    FileUploader,
  },
  data() {
    return {
      // 表单数据
      formData: {
        oss: [],
      },
      tableData: [],
      // 规则
      rules: {
        applicationReason: {
          rules: [
            {
              required: true,
              errorMessage: '请输入申请事由',
            },
          ],
        },
        goodsOwnUnit: {
          rules: [
            {
              required: true,
              errorMessage: '请输入所属单位',
            },
          ],
        },
        goodsOwnPerson: {
          rules: [
            {
              required: true,
              errorMessage: '请输入物品所属人',
            },
          ],
        },
        driverName: {
          rules: [
            {
              required: true,
              errorMessage: '请输入司机姓名',
            },
          ],
        },
        driverPhone: {
          rules: [
            {
              required: true,
              errorMessage: '请输入手机号',
            },
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码',
            },
          ],
        },
        carNumber: {
          rules: [
            {
              required: true,
              errorMessage: '请输入车牌号',
            },
          ],
        },
      },
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {},
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    console.log(query);
  },
  methods: {
    add() {
      this.$refs.popup.open('bottom');
    },
    del(i) {
      this.tableData.splice(i, 1);
    },
    onAddGoods(r) {
      this.tableData.push(r);
      this.$refs.popup.close();
    },
    // 提交
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then(async (res) => {
          console.log('success', res);
          // 如果提交过程中错误，那么通过存储的id直接开始工单流程
          if (this.formData.id) {
            console.log('工单提交', this.formData.id);
            this.onStartWorkFlow(this.formData);
            return;
          }
          // loading提示
          uni.showLoading({ title: '操作中' });
          // 提交接口
          let params = {
            ...res,
            ossIdArrStr: res.oss.map((item) => item.ossId).join(),
          };
          addPassPermit(params).then((res) => {
            this.formData.id = res.data.passPermitId;
            this.tableData.map((item) => {
              netPassPermitDetail({
                ...item,
                passPermitId: res.data.passPermitId,
              }).then((res) => {});
            });
            this.onStartWorkFlow(res.data);
          });
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 开启工单流
    onStartWorkFlow(data) {
      // loading提示
      uni.showLoading({ title: '生成工单中' });
      let params = {
        tableName: 'work_pass_permit',
        businessKey: data.id || data.passPermitId,
        variables: { entity: data },
      };
      netStartWorkFlow(params).then((res) => {
        this.onCompleteTask(res.data, params);
      });
    },
    // 完成工单流
    onCompleteTask(d, p) {
      let params = {
        taskId: d.taskId,
        variables: {},
        wfCopyList: [],
        messageType: ['1'],
        taskVariables: p.variables,
      };
      netCompleteTask(params).then((res) => {
        uni.hideLoading();
        uni.showToast({
          title: `提交成功`,
        });
        // 重置
        this.onReset();
      });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 20rpx 36rpx;
  padding-bottom: calc(var(--window-bottom) + 200rpx);
  display: flex;
  flex-direction: column;

  .alert {
    padding: 16rpx;
    margin: 24rpx 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12rpx;
    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 35%;
      color: #333333;
      font-size: 28rpx;
      font-weight: bold;
      position: relative;

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 60rpx);
    .btn {
      width: 100%;
      height: 92rpx;
      font-size: 40rpx;
      color: #ffffff;
      line-height: 92rpx;
      background: #3681ff;
    }
  }
}
</style>
